import spock.lang.Specification
import grails.test.mixin.TestFor
import org.glickr.FlickrService
import org.glickr.FlickrPhotoSet
import org.glickr.FlickrPhoto
import org.codehaus.groovy.grails.commons.cfg.ConfigurationHelper
import org.codehaus.groovy.grails.commons.ConfigurationHolder
import org.glickr.FlickrServiceSyntaxException
import org.glickr.FlickrServiceApiException

@TestFor(FlickrService)
class FlickrPhoto extends Specification{

    def setupSpec(){
        service.grailsApplication = new org.codehaus.groovy.grails.commons.DefaultGrailsApplication()
        grailsApplication.config.grails.plugins.glickr.apiKey    = '15bbb6ddbe424e19f1a6ab93ed9ace79'
        grailsApplication.config.grails.plugins.glickr.apiSecret = '09e0eaface56ff99'
    }

//    def "Finding a PhotoSet requires at least one parameter"() {
//        when: "Search without a parameter"
//        def result = service.findAll([])
//
//        then: "Should throw and exception"
//        thrown(FlickrServiceSyntaxException)
//    }

    def "Finding a PhotoSet based on free text "() {
        when: "Search on TEXT parameter for Kodak PCD0992 set"
        def result = service.findAll([text:'PCD0992'])

        then: "Should have a resultset"
        result instanceof FlickrPhotoSet

        and: "Result should have 24 records"
        result.photos.size() == 24

//        and: "Results shoud be photos"
//        result.photos.first() instanceof FlickrPhoto
    }


   def "getPhotoById needs an ID"() {
       when: "find without ID"
       def result = service.getPhotoById()

       then: "throws exception"
       thrown(FlickrServiceSyntaxException)
   }

    def "Find an existing image"() {
        when: "find with existing ID"
        def photo = service.getPhotoById(7955082398)

        then: "image found"
        photo instanceof org.glickr.FlickrPhoto

        and: "correct image found"
        photo.id == 7955082398
    }

}
